package com.example.kotlinandroid.http

import android.util.Log
import com.example.kotlin_retrofit.http.Api
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 接口请求工具类
 */
class HttpHelper {
    companion object {
        private val BASE_URL = "https://www.wanandroid.com/" //服务器地址
        private var sHttpClient: OkHttpClient? = null
        private var sApi: Api? = null

        /**
         * 添加拦截器，对返回的数据或者日志进行处理，对请求添加统一头部
         */
        fun getHttpClient(): OkHttpClient? {
            if (sHttpClient == null) {
                synchronized(HttpHelper::class.java) {
                    if (sHttpClient == null) {
                        val builder = OkHttpClient.Builder()
                        //添加拦截器
                        var interceptor = HttpLoggingInterceptor(
                            HttpLoggingInterceptor.Logger { message ->
                                //对日志进行处理
                                Log.e("Tag", message)
                            }
                        )
                        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
                        builder.addInterceptor(interceptor)
                        builder.addInterceptor { chain ->
                            val request = chain.request()
                            val compressedRequest =
                                request.newBuilder()
                                    .addHeader(
                                        "User-Agent",
                                        "Android"
                                    )
                                    .build()
                            chain.proceed(compressedRequest)
                        }
                        sHttpClient = builder
                            .callTimeout(40, TimeUnit.SECONDS)
                            .connectTimeout(40, TimeUnit.SECONDS)
                            .readTimeout(40, TimeUnit.SECONDS)
                            .writeTimeout(40, TimeUnit.SECONDS)
                            .build()
                    }
                }
            }
            return sHttpClient
        }

        /**
         * 获取接口服务
         */
        fun getApi(): Api? {
            if (sApi == null) {
                synchronized(HttpHelper::class.java) {
                    if (sApi == null) {
                        val retrofit = createRetrofit(BASE_URL)
                        sApi = retrofit.create(Api::class.java)
                    }
                }
            }
            return sApi
        }

        /**
         * 获取Retrofit对象，定义返回类型的转换器
         */
        private fun createRetrofit(baseUrl: String): Retrofit {
            val gsonBuilder = GsonBuilder()
            return Retrofit.Builder()
                .client(getHttpClient())
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()))
                .build()
        }
    }
}